package com.itheima.imclient100.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.itheima.imclient100.BaseActivity;
import com.itheima.imclient100.R;
import com.itheima.imclient100.adapter.ChatAdatper;
import com.itheima.imclient100.event.MessageEvent;
import com.itheima.imclient100.presenter.ChatPresenter;
import com.itheima.imclient100.presenter.impl.ChatPresenterImpl;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by fullcircle on 2017/7/4.
 *
 * 与别人的聊天界面
 */

public class ChatActivity extends BaseActivity implements ChatView{
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.tb_toolbar)
    Toolbar tbToolbar;
    @InjectView(R.id.rv_chat)
    RecyclerView rvChat;
    @InjectView(R.id.et_message)
    EditText etMessage;
    @InjectView(R.id.btn_send)
    Button btnSend;
    @InjectView(R.id.activity_main)
    LinearLayout activityMain;

    private ChatPresenter presenter;
    private ChatAdatper adapter;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        ButterKnife.inject(this);
        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        tvTitle.setText("与"+ username +"聊天中");
        initToolbar();

        presenter = new ChatPresenterImpl(this);

        //首先设置这个按钮不能够点击
        btnSend.setEnabled(false);


        //初始化recyclerview
        rvChat.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ChatAdatper(null);
        rvChat.setAdapter(adapter);


        //去数据库获取消息记录
        presenter.getHistoryMessage(username);

        etMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String msg = s.toString().trim();
                if(msg.length() == 0){
                    btnSend.setEnabled(false);
                }else{
                    btnSend.setEnabled(true);
                }
            }
        });
    }

    private void initToolbar() {
        tbToolbar.setTitle("");
        setSupportActionBar(tbToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @OnClick(R.id.btn_send)
    public void onClick() {
        //获取用户输入的文字内容
        String msg = etMessage.getText().toString().trim();
//        if (msg.equals("")){
//            btnSend.setEnabled(false);
//        }else {
            btnSend.setEnabled(true);
            //发送消息
            presenter.sendMessage(username, msg);
            //清空editText
            etMessage.setText("");
       // }
    }

    @Override
    public void onGetHistoryMessage(List<EMMessage> messageList) {
        if(messageList != null && messageList.size()>0){
            adapter.setMessageList(messageList);
            adapter.notifyDataSetChanged();
            rvChat.smoothScrollToPosition(messageList.size()-1);
        }
    }

    @Override
    public void onSendMessage(List<EMMessage> messageList) {
        if(messageList != null && messageList.size()>0){
            adapter.setMessageList(messageList);
            adapter.notifyDataSetChanged();
            rvChat.smoothScrollToPosition(messageList.size()-1);
        }
    }

    @Override
    public void updateMessageList() {
            adapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMsg(MessageEvent event){
        presenter.getHistoryMessage(username);

    }
    @Override
    public void onStart() {
        super.onStart();
        if(!EventBus.getDefault().isRegistered(this)){//加上判断
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().unregister(this);
        }
    }
}
